<?php
/**
 * Model de Artefato
 * @filesource  06/04/2011
 * @author      Estrada Virtual <http://estradavirtual.com.br>
 * @copyright   Copyright <2011> Estrada Virtual
 * @package     <<application>>
 * @subpackage  <<application>>.application.models
 * @version     $Id$
 */
class Model_Artefato extends App_Db_Table
{
    protected $_name             = 'artefato';
    protected $_primary          = array('id_artefato');
    protected $_referenceMap     = array(
                    'Cliente' => array(
                            'columns'       => 'fk_cliente',
                            'refTableClass' => 'Model_Cliente',
                            'refColumns'    => 'id_cliente',
                         ),
                    'Projeto' => array(
                            'columns'       => 'fk_projeto',
                            'refTableClass' => 'Model_Projeto',
                            'refColumns'    => 'id_projeto',
                         ),
                    'Servico' => array(
                            'columns'       => 'fk_servico',
                            'refTableClass' => 'Model_Servico',
                            'refColumns'    => 'id_servico',
                         ),
                    'Tarefa' => array(
                            'columns'       => 'fk_tarefa',
                            'refTableClass' => 'Model_Tarefa',
                            'refColumns'    => 'id_tarefa',
                         ),
                    'Usuario' => array(
                            'columns'       => 'fk_usuario',
                            'refTableClass' => 'Model_Usuario',
                            'refColumns'    => 'id_usuario',
                         ));

    public function upload(Zend_Db_Table_Row $row, $file)
    {
        $pathDir = $this->getPathDir($row);
        if (!file_exists($pathDir)) {
            mkdir($pathDir, 0777, true);
        }

        //$pathFile = $this->getPathFile($row);
        $pathFile = $pathDir . '/' . $file['name'];
        $tempFile = $file['tmp_name'];
        move_uploaded_file($tempFile, $pathFile);

        if (!file_exists($pathFile)) {
            throw new Exception('Não foi possivel enviar o artefato');
        }
        return $pathFile;
    }

    public function getPath()
    {
        return realpath(APPLICATION_PATH . '/../data/upload/artefato');
    }

    public function getPathDir(Zend_Db_Table_Row $row)
    {
        $path = $this->getPath();
        $path .= '/' . $row->fk_cliente;

        if ($row->fk_projeto) {
            $path .= '/projeto/' . $row->fk_projeto;
        }

        if ($row->fk_servico) {
            $path .= '/servico/' . $row->fk_servico;
        }

        if ($row->fk_tarefa) {
            $path .= '/tarefa/' . $row->fk_tarefa;
        }
        return $path;
    }

    public function getPathFile(Zend_Db_Table_Row $row)
    {
        $path = $this->getPathDir($row);
        $path .= '/' . $row->st_nome;
        return $path;
    }
}
